<template>
  <div class="container">
    <el-card shadow="never" class="pump-switch">
      <template slot="header">
        <div class="header-border">{{ PumpUpdateReqVO.name }} 目标阀位(%)</div>
      </template>
      <ul>
        <li><img v-if="PumpUpdateReqVO.valveValue===0" src="@/assets/images/pump.png" style="margin: 11px 0">
          <img v-else src="@/assets/images/pump1.png" style="margin: 11px 0">
        </li>
        <li class="slider">
          <el-slider v-model="PumpUpdateReqVO.valveValue" :marks="marks"></el-slider>
        </li>
        <li class="pump-value">{{ PumpUpdateReqVO.valveValue }}</li>
        <li>
          <el-button type="primary" style="background: #4362FF" @click="changePump()">确定</el-button>
        </li>
      </ul>
    </el-card>
      <div class="monitorList">
        <div class="monitor-panel" v-for="(item,index) in list" :key="index" ref="monitorPanel">
          <EZUIKitJs v-if="show" :id="item.id" :channelNo="item.channelNo" :deviceSerial="item.deviceSerial" :width="monitorWidth" :height="(monitorWidth * 2) / 3"></EZUIKitJs>
        </div>
      </div>
  </div>
</template>
<script>
import EZUIKitJs from '@/components/EZUIKitJs/index.vue'
import { updateInfo } from '@/api/equipment/waterPump'
import {debounce} from "throttle-debounce"

export default {
  components: {
    EZUIKitJs
  },
  data() {
    return {
      show: false,
      monitorWidth: 0,
      list: [
        {
          id: '64314e38654842359acd3d8a078b2a70',
          channelNo: 13,
          deviceSerial: 'L16595996'
        },
        {
          id: 'dde1086f119740ad9af2aed8acf57adb',
          channelNo: 8,
          deviceSerial: 'L16595996'
        }
      ],
      PumpUpdateReqVO: {
        id: this.$route.query.id,
        valveValue: Number(this.$route.query.valveValue),
        name: this.$route.query.name
      },
      marks: {
        0: '0',
        25: '25',
        50: '50',
        75: '75',
        100: '100'
      },
    }
  },
  mounted() {
    let monitorPanel = this.$refs.monitorPanel[0];
    this.monitorWidth = monitorPanel.offsetWidth;
    window.addEventListener("resize", this.resizeFn);
    this.show = true;
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeFn);
  },
  methods: {
    resizeFn: debounce(1000, function () {
      this.show = false;
      this.$nextTick(() => {
        let monitorPanel = this.$refs.monitorPanel[0];
        this.monitorWidth = monitorPanel.offsetWidth;
        this.show = true;
      });
    }),
    changePump() {
      updateInfo(this.PumpUpdateReqVO).then(res => {
        if (res.data) {
          this.$message.success('控制信息已发出')
        } else {
          this.$message.error('程序错误，请检查配置')
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  padding: 24px;
  background-color: rgb(240, 242, 245);
  height: calc(100vh - 86px);

  .pump-switch {
    background-color: #fff;
    border-radius: 8px;
    width: 100%;

    .header-border {
      padding-left: 15px;
      font-weight: bold;
      font-size: 16px;
      color: #333;
    }

    ul {
      margin: 0;
      padding: 0;
      height: 50px;
      width: 100%;
      list-style: none;
      line-height: 50px;

      li {
        float: left;
        padding-left: 30px;
      }

      li:last-child {
        float: right;
      }
      .pump-value{
        font-size: 16px;
        font-weight: bold;
      }
      .slider {
        width: calc(100% - 220px);
        ::v-deep .el-slider{
            height: 50px;
           .el-slider__runway{
            margin: 20px 0;
             height: 10px;
          }
          .el-slider__button-wrapper{
            height: 40px;
          }
          .el-slider__bar{
            background: #4362FF;
            height: 10px;
          }
          .el-slider__marks-text{
            height: 30px;
            line-height: 30px;
          }
          .el-slider__stop{
            height: 10px;
            width: 10px;
          }
        }
      }
    }
  }
  .monitorList {
    display: flex;
    flex-flow: wrap;

    .monitor-panel {
      width: calc((100% + 20px) / 2 - 20px);
      margin-right: 20px;
      border-radius: 12px;
      margin-top: 20px;
      overflow: hidden;

      &:nth-child(2n) {
        margin-right: 0px;
      }
    }
  }
}
</style>
